package 富婆梦.第一阶段.第五周.作业.俄罗斯转盘;

import java.util.Random;

/**
 * @ProjectName: java-learning
 * @Package: 富婆梦.第一阶段.第五周.作业.俄罗斯转盘
 * @ClassName: Gun
 * @Author: hemengjin
 * @Description: 手枪类，全是静态方法。全员公用一把手枪
 * @Date:2021/8/22 10:09
 * @Version: 1.0
 */
public class Gun {
    private static boolean[] dantang=new boolean[12];
    private static int needlel=0;

    private Gun(){

    }

    public static int getDantang(){
        return dantang.length;
    }

    public static int getNeedlel(){
        return needlel;
    }

    //转动转轮：确定开枪发射子弹的位置
    public static void turn(){
        needlel=new Random().nextInt(dantang.length-1);
    }

    //放子弹
    public static void putBullet(int index){
        if (dantang[index] == false){
            dantang[index] = true;
        }else {
            System.out.println("这个位子有子弹！换个位子放子弹");
        }
    }

    //膛内子弹的数量
    public static int getBullet(){
        int bullet = 0;
        for (boolean x:dantang){
            if (x==true){
                bullet = bullet+1;
            }
        }
        return bullet;
    }

    //开枪
    public static boolean shoot(){
        boolean be_shoot = false;
        if (dantang[needlel] == true){//有子弹
            dantang[needlel] = false;
            be_shoot = true;
            needlel = needlel+1;
        }else {//无子弹
            needlel = needlel+1;
            be_shoot = false;
        }
        if (needlel >= dantang.length){
            needlel = 0;
        }
        return be_shoot;
    }

    public static void main(String[] args) {
        //测试放子弹，子弹数量
        test1();
        //测试转动转轮
        test3();
        //测试开枪
        test2();
    }

    //往枪里面放子弹
    public static void test1(){
        for(int i = 0;i<3;i++){
            while (true){
                int x = new Random().nextInt(getDantang()-1);
                if (dantang[x] == false){
                    putBullet(x);
                    break;
                }
            }
        }
        /*System.out.println("膛内的子弹数量为："+getBullet());
        for (boolean a:dantang){
            System.out.println(a);
        }*/

    }
    //开枪
    public static void test2(){
        for (Boolean x:dantang){
            System.out.println("开枪前每个弹膛是否有子弹"+x);
        }
        for (int i = 0;i<12;i++){
            shoot();
        }
        System.out.println("--------------------------------");
        for (Boolean x:dantang){
            System.out.println("开枪后每个弹膛是否有子弹"+x);
        }
    }
    //转动转轮
    public static void test3(){
        System.out.println("转动转轮前的needle是："+getNeedlel());
        turn();
        System.out.println("转动转轮后的needle是："+getNeedlel());
    }
}
